package demo.xlj.yuyi.studentapp.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.TongZhiNewBean;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

public class LookAnnouncementMoreActivity extends BaseRxActivity {

    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.RecyclerView_Look_Announce_More)
    RecyclerView mRecyclerViewLookAnnounceMore;

    private BaseQuickAdapter<TongZhiNewBean.InfoBean, BaseViewHolder> mBaseQuickAdater;

//    private AnnouncenmentNewDao mAnnouncementNewDao;


    private List<TongZhiNewBean.InfoBean> mList_new;

    private String type_identity = "0";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_announcement_more);
        ButterKnife.bind(this);
        mTextViewTitle.setText("公告列表");
        Intent intent = getIntent();
        type_identity = intent.getStringExtra("type");
//        mAnnouncementNewDao = BaseApplication.instances.getDaoSession().getAnnouncenmentNewDao();
        initRecycler();
        //查询数据库
//        initData();
        //获取数据，从服务器
        haveData();
        initListener();

    }

    private void haveData() {
        mList_new = new ArrayList<>();
        addRxDestroy(RetrofitClient.getInstance(LookAnnouncementMoreActivity.this)
        .getApiService()
                .getTypeNotice(type_identity)
                .compose(RxSchedulers.<TongZhiNewBean>io_main())
                .subscribe(new Consumer<TongZhiNewBean>() {
                    @Override
                    public void accept(@NonNull TongZhiNewBean tongZhiNewBean) throws Exception {
                         if (tongZhiNewBean.getInfo().size() == 0){
                             mBaseQuickAdater.setEmptyView(R.layout.recycler_item_empty,mRecyclerViewLookAnnounceMore);
                             ToastUtil.success(LookAnnouncementMoreActivity.this,"暂时没有通知哦",false,true);
                         }else {
                             mList_new.addAll(tongZhiNewBean.getInfo());
                             mBaseQuickAdater.addData(mList_new);
                         }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mBaseQuickAdater.setEmptyView(R.layout.recycler_item_empty,mRecyclerViewLookAnnounceMore);
                    }
                })
        );
    }

    /**
     * 添加点击事件
     */
    private void initListener() {
        mBaseQuickAdater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String[] key = {"title", "org", "content"};
                String[] value = {mList_new.get(position).getTitle(), mList_new.get(position).getOrganization(), mList_new.get(position).getContent()};
                ActivityUtil.openActivityFromLeftToRightByMoreResuslt(LookAnnouncementMoreActivity.this, LookAnnouncementOtherInfo.class, key, value);
            }
        });
    }

    private void initData() {
//        mList = mAnnouncementNewDao.loadAll();
//        mBaseQuickAdater.addData(mList);
    }

    private void initRecycler() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(LookAnnouncementMoreActivity.this);
        mRecyclerViewLookAnnounceMore.setLayoutManager(layoutManager);
        mBaseQuickAdater = new BaseQuickAdapter<TongZhiNewBean.InfoBean, BaseViewHolder>(R.layout.recycler_look_announcement_item) {
            @Override
            protected void convert(BaseViewHolder helper, TongZhiNewBean.InfoBean item) {
                helper.setText(R.id.TextView_RecyclerView_look_announcement_Title, item.getTitle());
                helper.setText(R.id.TextView_RecyclerView_look_announcement_Time, item.getTime());
            }
        };
        mBaseQuickAdater.setEmptyView(R.layout.recycler_loading, mRecyclerViewLookAnnounceMore);
        mRecyclerViewLookAnnounceMore.setAdapter(mBaseQuickAdater);

    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        LookAnnouncementMoreActivity.this.finish();
    }
}
